import type { Router } from 'vue-router';
import setupUserLoginInfoGuard from "@/router/guard/userLoginInfo";
import {isTokenExpired} from "@/utils/jwtDecode";
import NProgress from "nprogress";

function setupPageGuard(router: Router) {
  router.beforeEach((to, from, next) => {
    NProgress.start();
    const token = localStorage.getItem('token');
    const isLoggedIn = token &&!isTokenExpired(token);
    if (to.meta.requiresAuth &&!isLoggedIn) {
      // 未登录或 token 已过期且访问需要认证的路由，重定向到登录页面
      next({ name: 'login' });
    } else {
      next();
    }
  });
}
export default function createRouteGuard(router: Router) {
  setupPageGuard(router);
  // setupUserLoginInfoGuard(router);
  // setupPermissionGuard(router);
}
